package com.evamay.kotlindemo.ui.main

import android.Manifest
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.app.Activity
import android.content.Intent
import android.os.Build

import android.os.Bundle
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager

import com.evamay.kotlindemo.R
import com.evamay.kotlindemo.entity.TypeInfo
import com.evamay.kotlindemo.ui.FileIo.FilesTestActivity
import com.evamay.kotlindemo.ui.rxdemo.RxMenuActivity
import com.evamay.kotlindemo.ui.viewpager.ViewPagerMenuActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initData()

    }

    private fun initData() {
        var list=ArrayList<TypeInfo>()
        list.add(TypeInfo(1,"文件、流"))
        list.add(TypeInfo(2,"RxAndroid"))
        list.add(TypeInfo(3,"ViewPager2"))//需要使用AndroidX，所有的配置都需要改成AndroidX版本
        var layoutManager= LinearLayoutManager(this)
        rv_main.layoutManager=layoutManager
        var myAdapter=MainAdapter(this,list)
        rv_main.adapter=myAdapter
        myAdapter.setClickListener(object :MainAdapter.OnItemClickListener{
            override fun onItemClick(position: Int) {

                when (list[position].type) {
                    1->{
                        if (Build.VERSION.SDK_INT >= 23) {
                            if (ContextCompat.checkSelfPermission(baseContext, Manifest.permission.READ_EXTERNAL_STORAGE) != PERMISSION_GRANTED) {
                                ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
                            } else {
                                var intent = Intent()
                                intent.setClass(baseContext, FilesTestActivity::class.java)
                                startActivity(intent)
                            }
                        } else {
                            var intent = Intent()
                            intent.setClass(baseContext, FilesTestActivity::class.java)
                            startActivity(intent)
                        }

                    }
                    2->{
                        var intent=Intent(baseContext,RxMenuActivity::class.java)
                        startActivity(intent)
                    }
                    3->{
                        var intent=Intent(baseContext,ViewPagerMenuActivity::class.java)
                        startActivity(intent)
                    }

                }
            }

        })

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>?, grantResults: IntArray?) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode==1) {
            for (i in 0..permissions!!.size-1) {
                if (grantResults!![i]!=PERMISSION_GRANTED) {
                    Toast.makeText(baseContext,"缺少${permissions[i]}权限",Toast.LENGTH_SHORT).show()
                    return
                }
            }
            var intent=Intent()
            intent.setClass(baseContext,FilesTestActivity::class.java)
            startActivity(intent)
        }
    }

}
